package com.kolon.business.mCafeteriaMenu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.DateUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mCafeteriaMenu.controller.MCafeteriaBoardController")
public class MCafeteriaBoardController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/business/mCafeteriaMenu/mCafeteriaBoard.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		// 기본식당 쿠키 추출
		Cookie[] cookies = request.getCookies();
		String resCookie = "";
		for(int i=0 ; i<cookies.length ; i++) {
			Cookie cookie = cookies[i];
			if(cookie.getName().equals("resCookieVal")) {
				resCookie = cookie.getValue();
				break;
			}
		}
		// 캘린더 관련 다음/이전/현재일 계산
		String YMD = ComUtil.getNullCheck(request.getParameter("YMD"), DateUtil.getToday());
		String year = ComUtil.getNullCheck(YMD.substring(0, 4), DateUtil.getYearStr());
		String month = ComUtil.getNullCheck(YMD.substring(5, 7), DateUtil.getMonthStr());
		String day = ComUtil.getNullCheck(YMD.substring(8, 10), DateUtil.getDayStr());
		String CafeCD = "1";
		if(!"".equals(request.getParameter("res") == null ? "" : request.getParameter("res"))) {
			CafeCD = request.getParameter("res"); 
		}else if(!"".equals(resCookie)&& "".equals(request.getParameter("res") == null ? "" : request.getParameter("res"))) {
			CafeCD = resCookie;
		}else if("".equals(resCookie) && "".equals(request.getParameter("res") == null ? "" : request.getParameter("res"))) {
			CafeCD = "1";
		}
		// URL 생성
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/cafeteria_menu.aspx?Gubun=").append("RestOpinionList");
		buffer.append("&CafeCD=").append(CafeCD);
		buffer.append("&inYMD=").append(YMD);
		buffer.append("&userid=").append(id);
		buffer.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("RestOpinionList");
		Iterator itXml = xmlList.iterator();
		Map map = null;
		List mapList = new ArrayList(); 
		// XML 데이터 
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("OpinionCD", el.getChild("OpinionCD").getText());
			map.put("UserID", el.getChild("UserID").getText());
			map.put("UserName", el.getChild("UserName").getText());
			map.put("Opinion", el.getChild("Opinion").getText());
			map.put("CreateDateY", el.getChild("CreateDate").getText().substring(0, 4));
			map.put("CreateDateM", el.getChild("CreateDate").getText().substring(5, 7));
			map.put("CreateDateD", el.getChild("CreateDate").getText().substring(8, 10));
			map.put("EmoID", el.getChild("EmoID").getText());
			map.put("Url", el.getChild("Url").getText());
			mapList.add(map);
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("year", year);
		modelAndView.addObject("month", month);
		modelAndView.addObject("day", day);
		modelAndView.addObject("resCookie", resCookie);		
		modelAndView.addObject("res", CafeCD);
		return modelAndView;
	}

}